package com.twu.biblioteca.ui;

import com.twu.biblioteca.domain.Book;
import com.twu.biblioteca.domain.ItemNotFoundException;
import com.twu.biblioteca.domain.Repository;
import com.twu.biblioteca.io.IO;

public class ReserveBookMenuItemHandler implements MenuItemHandler {

    Repository<Book> bookRepository;
    IO io;

    public ReserveBookMenuItemHandler(Repository<Book> bookRepository, IO io) {
        this.bookRepository = bookRepository;
        this.io = io;
    }

    @Override
    public void handle() {
        try {
            String idToReserve = io.promptForInput("Enter item id to reserve:");
            boolean canReserve = bookRepository.reserve(idToReserve);

            if (!canReserve) {
                io.writeOutput("Sorry we don't have that book yet.");
            } else {
                io.writeOutput("Thank You! Enjoy the book.");
            }
        } catch (ItemNotFoundException e) {
            io.writeOutput(e.getMessage());
        }
    }

}
